package com.qf.fmall2204.shopcart.controller;

import com.qf.fmall2204.constants.Constants;
import com.qf.fmall2204.shopcart.entity.ShoppingCart;
import com.qf.fmall2204.shopcart.service.IShoppingCartService;
import com.qf.fmall2204.vo.ResultVo;
import io.swagger.models.auth.In;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 购物车  前端控制器
 * </p>
 *
 * @author dong
 * @since 2023-04-24
 */
@CrossOrigin
@RestController
@RequestMapping("/shopcart")
@Slf4j
public class ShoppingCartController {

    @Autowired
    IShoppingCartService shoppingCartService;

    @GetMapping("/listbycids")
    public ResultVo listbycids(Integer[] cids){

        List<ShoppingCart> result = shoppingCartService.listbycids(cids);

        return new ResultVo(Constants.SUCCESS_CODE, "success", result);
    }


    @PutMapping("/update/{cartId}/{num}")
    public ResultVo update(@PathVariable("cartId") Integer cartId,@PathVariable("num") Integer num ){

        shoppingCartService.updateCartNumById(cartId,num);

        return new ResultVo(Constants.SUCCESS_CODE, "success", null);

    }

    @GetMapping("/list")
    public ResultVo list(Integer userId){

        List<ShoppingCart> result = shoppingCartService.listShopcart(userId);

        return new ResultVo(Constants.SUCCESS_CODE, "success", result);
    }

    /**
     * post请求体中传递的是一个json，格式如下
     * {"cartNum":1,"productId":"3","productPrice":1,"skuId":"1","skuProps":"口味:原味;包装:盒装;","userId":"20"}
     * @return
     */
    @PostMapping("/add")
    public ResultVo add(@RequestBody ShoppingCart cart){// @RequestBody将请求体中的json转换为 cart对象
        log.debug("入参为:cart={}",cart);

        shoppingCartService.add(cart);

        return new ResultVo(Constants.SUCCESS_CODE, "success",null);

    }

}
